﻿using System;

namespace Work2
{
    public class Student
    {
        public Student(int sno, string name)
        {
            this.Sno = sno;
            this.Name = name;
        }
        public int Sno
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
    }
    class Class
    {
        Student[] _student;
        public Class(int capacity)
        {
            _student = new Student[capacity];
        }
        public Student this[int index]
        {
            get
            {
                if (index < 0 || index >= _student.Length)
                {
                    Console.WriteLine("索引无效");
                    return null;
                }
                return _student[index];
            }
            set
            {
                if (index < 0 || index >= _student.Length)
                {
                    Console.WriteLine("索引无效");
                    return;
                }
                _student[index] = value;
            }
        }
        public Student this[string name]
        {
            get
            {
                foreach (Student s in _student)
                {
                    if (s.Name == name)
                        return s;
                }
                Console.WriteLine("未找到");
                return null;
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            Class collectively = new Class(3);
            Student first = new Student(20221, "小白");
            Student second = new Student(20222, "小黑");
            Student third = new Student(20223, "黑白");
            collectively[0] = first;
            collectively[1] = second;
            collectively[2] = third;
            Student objStudent1 = collectively[1];
            Console.WriteLine("学生学号：{0}，学生姓名：{1}", objStudent1.Sno, objStudent1.Name);
            Student objStudent2 = collectively["小白"];
            Console.WriteLine("学生小白学号：{0}", objStudent2.Sno);
            Console.ReadLine();
        }
    }
}